package ar.com.ford.it.fullEconomics.facade;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.Vector;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.logic.CommodityLogic;
import ar.com.ford.it.fullEconomics.logic.CommodityPartLogic;
import ar.com.ford.it.fullEconomics.logic.PartLogic;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp02Commodity;
import ar.com.ford.it.fullEconomics.struct.OptionCombo;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;

public class CommodityFacade {

    /**
     * Obtiene los productos por codigo
     * 
     * @param commodityCode
     * @return
     * @throws FullEconomicsException
     */
    public Teesp02Commodity findCommodityByName(String commodityName,
                                                Teesg01Plant plant)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();
        Teesp02Commodity result = new Teesp02Commodity();
        result = logic.findCommodityByName(commodityName, plant);
        return result;
    }

    /**
     * Obtiene los productos por codigo
     * 
     * @param commodityCode
     * @return
     * @throws FullEconomicsException
     */
    public Teesp02Commodity findCommodityByCode(String commodityCode,
                                                Teesg01Plant plant)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();
        Teesp02Commodity result = new Teesp02Commodity();
        result = logic.findCommodityByCode(commodityCode, plant);
        return result;
    }

    /**
     * Da de alta el Commodity pasado por parametro
     * 
     * @param commodity
     * Commodity a dar de alta
     * @param user
     * Usuario que da de alta el Commodity
     * @throws FullEconomicsException
     */
    public void createCommodity(Teesp02Commodity commodity,
                                FullEconomicsWslxUser user)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();

        logic.createCommodity(commodity, user);
    }

    /**
     * Actualiza el Commodity pasado por parametro
     * 
     * @param commodity
     * Commodity a actualizar
     * @param user
     * Usuario que actualiza el Commodity
     * @throws FullEconomicsException
     */
    public void updateCommodity(Teesp02Commodity commodity,
                                FullEconomicsWslxUser user)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();

        logic.updateCommodity(commodity, user);
    }

    /**
     * Elimina el commodity pasado por parametro
     * 
     * @param commodity
     * Commodity a eliminar
     * @throws FullEconomicsException
     */
    public void deleteCommodity(Teesp02Commodity commodity)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();

        logic.deleteCommodity(commodity);
    }

    /**
     * Busca Commoditys segun code
     * 
     * @param code
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesp02Commodity> findCommodities(String code,
                                                        String name,
                                                        Teesg01Plant plant)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();
        Collection<Teesp02Commodity> ret = logic.findCommodities(code, name,
                plant);

        return ret;
    }

    /**
     * Busca Commoditys el codigo de las partes que contiene
     * 
     * @param code
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesp02Commodity> findCommoditiesByParts(
                                                               String prefix,
                                                               String base,
                                                               String suffix,
                                                               Teesg01Plant plant)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();
        Collection<Teesp02Commodity> ret = logic.findCommoditiesByParts(prefix,
                base, suffix, plant);

        return ret;
    }

    /**
     * busca lo elementos relacionados de el commodity pasado por parametro
     * 
     * @param commodity
     * Commodity a eliminar
     * @throws FullEconomicsException
     */
    public String[] commoditysRelatedElements(Teesp02Commodity commodity)
            throws FullEconomicsException {
        CommodityLogic logic = new CommodityLogic();
        String[] commRelatedElementsIDs;
        commRelatedElementsIDs = logic.commoditysRelatedElements(commodity);
        return commRelatedElementsIDs;
    }

    /**
     * Crea una relacion entre el commodity y la prate pasados por parametro
     * 
     * @param commodity
     * @param part
     * @param plant
     * @throws FullEconomicsException
     */
    public void createCommPart(String commodity, Teesp01Part part,
                               FullEconomicsWslxUser user)
            throws FullEconomicsException {
        PartLogic partLogic = new PartLogic();
        CommodityLogic commodityLogic = new CommodityLogic();
        CommodityPartLogic commPartLogic = new CommodityPartLogic();
        Teesp01Part dbPart = partLogic.findPartByPk(part);
        Teesp02Commodity dbCommodity = commodityLogic.findCommodityByCode(
                commodity, part.getEesp01PlantCodeC());

        commPartLogic.createCommPart(dbCommodity, dbPart, user);
    }

    public void closeCommPart(Teesp02Commodity commodity, Teesp01Part part,
                              FullEconomicsWslxUser user)
            throws FullEconomicsException {

        CommodityPartLogic logic = new CommodityPartLogic();
        logic.closeCommPart(commodity, part, user);
    }

    /**
     * Inserta todos los commoditys pasados en la colleccion en el commodity.
     * 
     * @param commodity
     * @param parts
     * @param plant
     * @throws FullEconomicsException
     */
    public void createCommodParts(Teesp02Commodity commodity,
                                  Collection<Teesp01Part> parts,
                                  FullEconomicsWslxUser user)
            throws FullEconomicsException {
        PartLogic partLogic = new PartLogic();
        CommodityPartLogic commPartLogic = new CommodityPartLogic();
        Collection<Teesp01Part> dbParts = new Vector<Teesp01Part>();
        for (Teesp01Part part : parts) {
            part.setEesp01PlantCodeC(user.getTeesg01Plant());
            dbParts.add(partLogic.findPartByPk(part));
        }
        commPartLogic.createCommParts(commodity, dbParts, user);
    }

    /**
     * Devuelve el producto al cual pertenece la pieza en la fecha actual
     * 
     * @param part
     * @return
     * @throws FullEconomicsException
     */
    public Teesp02Commodity getPartsEffectiveCommodity(Teesp01Part part)
            throws FullEconomicsException {
        CommodityPartLogic commPartsLogic = new CommodityPartLogic();
        Teesp02Commodity effectiveCommodity = commPartsLogic
                .getPartsEffectiveCommodity(part);
        return effectiveCommodity;
    }

    /**
     * Obtiene una pieza por su pk
     * 
     * @param part
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Teesp01Part findPartByPk(Teesp01Part part)
            throws FullEconomicsException {
        PartLogic logic = new PartLogic();
        Teesp01Part result = new Teesp01Part();
        result = logic.findPartByPk(part);
        return result;
    }

    /**
     * Obtiene todas las clases de partes existentes
     * 
     * @return
     * @throws FullEconomicsException
     */
    public Collection<OptionCombo> getPartClases() {
        PartLogic logic = new PartLogic();
        Collection<OptionCombo> result = logic.getPartClases();
        return result;
    }

    /**
     * Da de alta el Part pasado por parametro
     * 
     * @param part
     * Part a dar de alta
     * @param user
     * Usuario que da de alta el Part
     * @throws FullEconomicsException
     */
    public void createPart(Teesp01Part part, FullEconomicsWslxUser user)
            throws FullEconomicsException {
        PartLogic logic = new PartLogic();

        logic.createPart(part, user);
    }

    /**
     * Actualiza el Part pasado por parametro
     * 
     * @param part
     * Part a actualizar
     * @param user
     * Usuario que actualiza el Part
     * @throws FullEconomicsException
     */
    public void updatePart(Teesp01Part part, FullEconomicsWslxUser user)
            throws FullEconomicsException {
        PartLogic logic = new PartLogic();

        logic.updatePart(part, user);
    }

    /**
     * Elimina la pieza pasada por parametro
     * 
     * @param part
     * Part a eliminar
     * @throws FullEconomicsException
     */
    public void deletePart(Teesp01Part part) throws FullEconomicsException {
        PartLogic logic = new PartLogic();

        logic.deletePart(part);
    }

    /**
     * Busca Parts segun vairos parametros de busqeuda
     * 
     * @param findPart
     * @param dateInMax
     * @param dateOutMax
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesp01Part> findParts(Teesp01Part findPart,
                                             Timestamp dateInMax,
                                             Timestamp dateOutMax)
            throws FullEconomicsException {
        PartLogic logic = new PartLogic();
        Collection<Teesp01Part> ret = logic.findParts(findPart, dateInMax,
                dateOutMax);

        return ret;
    }

    /**
     * Busca Parts segun supplier, clase, prefijo, base, sufijo, y rango de
     * fechas in y out
     * 
     * @param findPart
     * @param dateInMax
     * @param dateOutMax
     * @param supplierClass
     * @param supplierCode
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesp01Part> findParts(Teesp01Part findPart,
                                             Timestamp dateInMax,
                                             Timestamp dateOutMax,
                                             String supplierClass,
                                             String supplierCode)
            throws FullEconomicsException {
        PartLogic logic = new PartLogic();
        Collection<Teesp01Part> ret = logic.findParts(findPart, dateInMax,
                dateOutMax, supplierClass, supplierCode);

        return ret;
    }

    /**
     * Busca Parts segun low commodity
     * 
     * @param findPart
     * @param dateInMax
     * @param dateOutMax
     * @param supplierClass
     * @param supplierCode
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesp01Part> finLowCommodityParts(String lowCom,
                                                        Teesg01Plant plant)
            throws FullEconomicsException {
        PartLogic logic = new PartLogic();
        Collection<Teesp01Part> ret = logic.finLowCommodityParts(lowCom, plant);

        return ret;
    }

    /**
     * Verifica si la parte se puede borrar.
     * 
     * @param part
     * @return
     * @throws FullEconomicsException
     */
    public boolean isDeleteable(Teesp01Part part) throws FullEconomicsException {
        PartLogic logic = new PartLogic();
        boolean isDel = logic.isDeleteable(part);
        return isDel;
    }

    /**
     * Elimina la pieza pasada por parametro y sus elementos relacionados
     * 
     * @param part
     * Part a eliminar
     * @throws FullEconomicsException
     */
    public void cascadeDelete(Teesp01Part part) throws FullEconomicsException {
        PartLogic logic = new PartLogic();

        logic.cascadeDelete(part);
    }

    /**
     * Busca Parts segun supplier, clase, prefijo, base, sufijo, y rango de
     * fechas in y out
     * 
     * @param findPart
     * @param dateInMax
     * @param dateOutMax
     * @param supplierClass
     * @param supplierCode
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesp01Part> findCommodityParts(Teesp02Commodity commodity)
            throws FullEconomicsException {
        CommodityPartLogic logic = new CommodityPartLogic();
        Collection<Teesp01Part> ret = logic.findCommodityParts(commodity);
        return ret;
    }

    /**
     * Busca Parts sin cost structure. las filtra por parte, proveedor, comodity
     * y linea de vehiculo
     * 
     * @param findPart
     * @param dateInMax
     * @param dateOutMax
     * @param supplierClass
     * @param supplierCode
     * @param plant
     * @return
     * @throws FullEconomicsException
     */
    public Collection<Teesp01Part> findPartsWithoutCostStr(
                                                           Teesp01Part findPart,
                                                           Timestamp dateInMax,
                                                           Timestamp dateOutMax,
                                                           String supplierClass,
                                                           String supplierCode,
                                                           String commodity,
                                                           String vehicleLine)
            throws FullEconomicsException {
        PartLogic logic = new PartLogic();
        Collection<Teesp01Part> ret = logic.findPartsWithoutCostStr(findPart,
                dateInMax, dateOutMax, supplierClass, supplierCode, commodity,
                vehicleLine);

        return ret;
    }
}
